﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class Requisition_Receipt : System.Web.UI.Page
    {
        /// <summary>
        /// Fills in part of the html title tag
        /// </summary>
        public String description { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            int rID;

            if (Session["RequisitionID"] != null)
            {
                rID = (int)Session["RequisitionID"];
                BindReceipt(rID);
            }
            else
            {
                lblMasterError.Text = "Sorry, we lost track of the Requisition to report. Try logging out, logging back in and request this report again.";
            }
        }

        private void BindReceipt(int rID)
        {
            Presentation presentation = new Presentation();
            DataSet ds = presentation.GetReceiptVoucher(rID);

            //this does grid and LPO label
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                //TODO: Still need to retrieve receiving officer.
                gvReceiptVoucher.DataSource = ds.Tables[1];
                gvReceiptVoucher.DataBind();
                String lpo = ds.Tables[1].Rows[0]["LPO"].ToString();
                lblViewLPO.Text = lpo;
                lblViewOfficer.Text = ds.Tables[1].Rows[0]["ReceivingOfficer"].ToString();
                int vendorid = Convert.ToInt32(ds.Tables[1].Rows[0]["VendorID"].ToString());

                ds = presentation.GetVendorByID(vendorid);
                //this does vendor labels
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    lblViewVendor.Text = ds.Tables[1].Rows[0]["vendorName"].ToString();
                    lblViewVendorAddress.Text = ds.Tables[1].Rows[0]["vendorAddress"].ToString();
                    lblViewVendorPhone.Text = ds.Tables[1].Rows[0]["phoneNumber"].ToString();
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                //get one more label filled.
                ds = presentation.GetStoreByID(GetStoreID());
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    lblViewRequestingStore.Text = ds.Tables[1].Rows[0]["storeName"].ToString();
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
            }
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
        }

        /// <summary>
        /// Takes evaluations of columns in a data bound GridView to calculate cost * qty for a row.
        /// </summary>
        /// <param name="cost">Cost of an item in a GridViewRow.</param>
        /// <param name="qty">Quantity of an item in a GridViewRow.</param>
        /// <returns>String formatted value of cost * qty.</returns>
        public String CalcCost(Object cost, Object qty)
        {
            //DataBind functions always pass in generic objects, not strongly typed objects
            Double dCost = Convert.ToDouble(cost);
            Double dQty = Convert.ToDouble(qty);

            return String.Format("{0:0.00}", (dCost * dQty));
        }

        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
    }
}